package com.clothe.service;

import java.sql.Connection;
import java.util.List;

import com.clothe.dao.IClothingDAO;
import com.clothe.dao.impl.ClothingDAOImpl;
import com.clothe.database.DataBase;
import com.clothe.dto.Clothing;

public class ClothingService {

	IClothingDAO clothingDAOImpl = new ClothingDAOImpl();

	public boolean save(Clothing clothing) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			flag = clothingDAOImpl.save(clothing);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public boolean delete(int id) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			flag = clothingDAOImpl.delete(id);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public boolean update(Clothing clothing) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			flag = clothingDAOImpl.update(clothing);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}
	
	public boolean updates(Clothing clothing) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			flag = clothingDAOImpl.updates(clothing);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public Clothing get(int id) throws Exception {
		Connection connection = null;
		Clothing clothing = null;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			clothing = clothingDAOImpl.get(id);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return clothing;
	}

	public Clothing getbytypeid(int typeid) throws Exception {
		Connection connection = null;
		Clothing clothing = null;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			clothing = clothingDAOImpl.get(typeid);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return clothing;
	}

	public List<Clothing> list(int pageSize, int pageNo,String queryString) throws Exception {
		Connection connection = null;
		List<Clothing> clothinglist = null;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			clothinglist = clothingDAOImpl.list(pageSize, pageNo,queryString);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return clothinglist;
	}

	public int getCount(String queryString) throws Exception {
		int totalpage = 0;
		Connection connection = null;
		try {
			connection = DataBase.getConn();
			clothingDAOImpl.setConn(connection);
			totalpage = clothingDAOImpl.getCount(queryString);
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {

			DataBase.releaseConnection(connection);
		}
		return totalpage;

	}
}
